/**
 * 初始化题详情对话框
 */
var QuestionInfoDlg = {
    questionInfoData: {},
    validateFields: {
        degree: {
            validators: {
                regexp: {
                    regexp: /^[0-9]+$/,
                    message: '难度只能输入正整数'
                }, notEmpty: {
                    message: '难度不能为空'
                }
            }
        },
        info: {
            validators: {
                notEmpty: {
                    message: '题目信息不能为空'
                },
                stringLength: {
                    min: 1,
                    max: 5000,
                    message: '题目信息字数必须在1到5000之间'
                }
            }
        }
        //, answer1: {
        //     validators: {
        //         notEmpty: {
        //             message: '答案1不能为空'
        //         }
        //     }
        // },answer2: {
        //     validators: {
        //         notEmpty: {
        //             message: '答案2不能为空'
        //         }
        //     }
        // },answer3: {
        //     validators: {
        //         notEmpty: {
        //             message: '答案3不能为空'
        //         }
        //     }
        // },answer4: {
        //     validators: {
        //         notEmpty: {
        //             message: '答案4不能为空'
        //         }
        //     }
        // }
    }

};

/**
 * 清除数据
 */
QuestionInfoDlg.clearData = function () {
    this.questionInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
QuestionInfoDlg.set = function (key, val) {
    this.questionInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
QuestionInfoDlg.get = function (key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
QuestionInfoDlg.close = function () {
    parent.layer.close(window.parent.Question.layerIndex);
}

/**
 * 收集数据
 */
QuestionInfoDlg.collectData = function () {
    this.set('id').set('categoryid').set('info').set('degree').set('remarks')
        .set('answer1').set('answer2').set('answer3').set('answer4').set('rightAnswer');
}

/**
 * 提交添加
 */
QuestionInfoDlg.addSubmit = function () {

    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    var info = {};
    var infoText = this.questionInfoData.info;
    var answer1 = this.questionInfoData.answer1;
    var answer2 = this.questionInfoData.answer2;
    var answer3 = this.questionInfoData.answer3;
    var answer4 = this.questionInfoData.answer4;
    var rightAnswer = new Number(this.questionInfoData.rightAnswer);
    info.infoText = infoText;
    var answers = [];
    answers.push(answer1);
    answers.push(answer2);
    answers.push(answer3);
    answers.push(answer4);
    info.answers = answers;
    info.rightAnswer = rightAnswer;
    this.questionInfoData.info = JSON.stringify(info);
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/question/add", function (data) {
        Feng.success("添加成功!");
        window.parent.Question.table.refresh();
        QuestionInfoDlg.close();
    }, function (data) {
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.questionInfoData);
    ajax.start();
}

/**
 * 提交修改
 */
QuestionInfoDlg.editSubmit = function () {

    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    var info = {};
    var infoText = this.questionInfoData.info;
    var answer1 = this.questionInfoData.answer1;
    var answer2 = this.questionInfoData.answer2;
    var answer3 = this.questionInfoData.answer3;
    var answer4 = this.questionInfoData.answer4;
    var rightAnswer = new Number(this.questionInfoData.rightAnswer);
    info.infoText = infoText;
    var answers = [];
    answers.push(answer1);
    answers.push(answer2);
    answers.push(answer3);
    answers.push(answer4);
    info.answers = answers;
    info.rightAnswer = rightAnswer;
    this.questionInfoData.info = JSON.stringify(info);
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/question/update", function (data) {
        Feng.success("修改成功!");
        window.parent.Question.table.refresh();
        QuestionInfoDlg.close();
    }, function (data) {
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.questionInfoData);
    ajax.start();
}
QuestionInfoDlg.onClickDept = function (e, treeId, treeNode) {
    $("#category").attr("value", instance.getSelectedVal());
    $("#categoryid").val(treeNode.id);
};

/**
 * 显示部门选择的树
 *
 * @returns
 */
QuestionInfoDlg.showCategorySelectTree = function () {
    var categoryObj = $("#category");
    var categoryPostion = $("#category").offset();
    $("#menuContent").css({
        left: categoryPostion.left + "px",
        top: categoryPostion.top + categoryObj.outerHeight() + "px"
    }).slideDown("fast");

    $("body").bind("mousedown", onBodyDown);
};

/**
 * 显示用户详情部门选择的树
 *
 * @returns
 */
QuestionInfoDlg.showInfoCategorySelectTree = function () {
    var categoryObj = $("#category");
    var categoryPostion = $("#category").position();
    $("#menuContent").css({
        left: categoryPostion.left + "px",
        top: categoryPostion.top + categoryObj.outerHeight() + "px"
    }).slideDown("fast");

    $("body").bind("mousedown", onBodyDown);
};

/**
 * 隐藏部门选择的树
 */
QuestionInfoDlg.hideCategorySelectTree = function () {
    $("#menuContent").fadeOut("fast");
    $("body").unbind("mousedown", onBodyDown);// mousedown当鼠标按下就可以触发，不用弹起
};

function onBodyDown(event) {
    if (!(event.target.id == "menuBtn" || event.target.id == "menuContent" || $(
            event.target).parents("#menuContent").length > 0)) {
        QuestionInfoDlg.hideDeptSelectTree();
    }
}
QuestionInfoDlg.hideDeptSelectTree = function () {
    $("#menuContent").fadeOut("fast");
    $("body").unbind("mousedown", onBodyDown);// mousedown当鼠标按下就可以触发，不用弹起
};

/**
 * 验证数据是否为空
 */
QuestionInfoDlg.validate = function () {
    $('#questionInfoForm').data("bootstrapValidator").resetForm();
    $('#questionInfoForm').bootstrapValidator('validate');
    return $("#questionInfoForm").data('bootstrapValidator').isValid();
};

$(function () {
    Feng.initValidator("questionInfoForm", QuestionInfoDlg.validateFields);

    var ztree = new $ZTree("treeDemo", "/questionCategory/tree");
    ztree.bindOnClick(QuestionInfoDlg.onClickDept);
    ztree.init();
    instance = ztree;
});
